﻿using RightControl.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace RightControl.WebApp {
    public static class MyExtHtmlLabel {
        /// <summary>
        /// 菜单管理权限复选框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="_list"></param>
        /// <returns></returns>
        public static HtmlString ActionCheckBox(this HtmlHelper helper, dynamic _list = null, dynamic _alist = null) {
            StringBuilder sb = new StringBuilder();
            var list = _list as List<ActionModel>;
            var alist = _alist as List<ActionModel>;
            if (list != null && list.Count > 0) {
                foreach (var v in list) {
                    bool isSelect = false;
                    foreach (var item in alist) {
                        if (v.ActionCode == item.ActionCode) {
                            isSelect = true;
                        }
                    }
                    sb.AppendFormat(@"<input type='checkbox' lay-skin='primary' name='{0}' title='{1}' value='{2}' {3}>", v.ActionCode, v.ActionName, v.Id, isSelect ? "checked" : "");
                }
            }
            return new HtmlString(sb.ToString());
        }
        public static HtmlString DisplayStatusHtml(this HtmlHelper helper, bool? value) {
            var msg = value.Value ? "启用" : "停用";
            return new HtmlString(string.Format("<span>{0}</span>", msg));
        }
        public static HtmlString StatusRadioHtml(this HtmlHelper helper, bool? value) {
            var msg = value.Value ? "启用" : "停用";
            string enabledStatus = value.Value ? "checked" : "";
            string disabledStatus = value.Value ? "" : "checked";

            string result = string.Format(@"<input name = ""Status"" value = ""true"" title = ""启用"" {0} type = ""radio"" >
                                        <div class=""layui-unselect layui-form-radio layui-form-radioed""><i class=""layui-anim layui-icon""></i><div>启用</div></div>
                                          <input name = ""Status"" value=""false"" title=""停用"" type=""radio"" {1}>
                                        <div class=""layui-unselect layui-form-radio""><i class=""layui-anim layui-icon""></i><div>停用</div></div>", enabledStatus, disabledStatus);

            return new HtmlString(result);
        }
        /// <summary>
        /// 状态下拉框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="defaultTxt">默认显示文本</param>
        /// <returns></returns>
        public static HtmlString StatusSelectHtml(this HtmlHelper helper, string defaultTxt = "") {
            return new HtmlString(string.Format(@"<select name='Status'>
                            <option value = ''>{0}</option >
                            <option value = 'true'> 启用 </option>
                            <option value = 'false'> 停用 </option>
                             </select>", defaultTxt));
        }
        /// <summary>
        /// 性别下拉框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="defaultTxt">默认显示文本</param>
        /// <returns></returns>
        public static HtmlString GanderRadioHtml(this HtmlHelper helper, int defaultVal = 1) {
            var _male = defaultVal == 1 ? "checked" : "";
            var _female = defaultVal == 0 ? "checked" : "";
            return new HtmlString(string.Format(@"<input type='radio' name='Gender' value='1' title='男' {0}>
                     <input type='radio' name='Gender' value='0' title='女' {1}>", _male, _female));
        }
        /// <summary>
        /// 搜索按钮
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static HtmlString SearchBtnHtml(this HtmlHelper helper, string txt = "搜索", string _class = "") {
            return new HtmlString(string.Format("<a href='javascript:;' class='layui-btn{1}' id='btnSearch' data-type='reload'><i class='layui-icon'>&#xe615;</i>{0}</a>", txt, _class));
        }
        /// <summary>
        /// 重置按钮
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static HtmlString ResetBtnHtml(this HtmlHelper helper, string txt = "重置", string _class = " layui-btn-primary") {
            return new HtmlString(string.Format("<button type='reset' class='layui-btn{1}'>{0}</button>", txt, _class));
        }
        /// <summary>
        /// 表单内工具栏
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static HtmlString ToolBarHtml(this HtmlHelper helper, dynamic _list = null) {
            StringBuilder sb = new StringBuilder();
            //sb.Append("<script type='text/html' id='bar'>");
            var list = _list as List<ActionModel>;
            if (list != null && list.Count > 0) {
                foreach (var v in list) {
                    var _icon = string.IsNullOrEmpty(v.Icon) ? "" : string.Format("<i class='layui-icon iconfont {0}'></i>", v.Icon);
                    sb.AppendFormat("<a class='layui-btn layui-btn-xs {0}' lay-event='{1}'>{3}{2}</a>", v.ClassName, v.Method, v.ActionName, _icon);
                }
            }
            //sb.Append("</script>");
            return new HtmlString(sb.ToString());
        }
        /// <summary>
        /// 表单外工具栏
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static HtmlString TopToolBarHtml(this HtmlHelper helper, dynamic _list = null, string initTxt = null) {
            StringBuilder sb = new StringBuilder();
            var list = _list as List<ActionModel>;
            if (list != null && list.Count > 0) {
                foreach (var v in list) {
                    sb.AppendFormat(@"<a href='javascript:;' class='layui-btn {0}' id='btn{1}'>
                            <i class='layui-icon iconfont {2}'></i> {3}
                        </a>", v.ClassName, v.ActionCode, v.Icon, initTxt == null ? v.ActionName : initTxt);
                }
            }
            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// 表单内工具栏
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static HtmlString CategoryHtml(this HtmlHelper helper, dynamic _list = null, string selectVal = "") {
            StringBuilder sb = new StringBuilder(string.Format(@"<select id='selCategoryCode'  lay-filter='category' name='CategoryCode'>
                                                                <option value = ''>请选择</option >"));
            var list = _list as List<BaseCategory>;
            if (list != null && list.Count > 0) {
                string selectProp = "";
                for (int i = 0; i < list.Count; i++) {
                    BaseCategory v = list[i];
                    selectProp = "";
                    if (string.IsNullOrEmpty(selectVal)) {
                        if (i == 0) {
                            selectProp = "selected";
                        }
                    }
                    else {
                        selectProp = selectVal == v.CategoryCode ? "selected" : "";
                    }

                    sb.AppendFormat("<option value = '{0}' {2}> {1} </option>", v.CategoryCode, v.Description, selectProp);
                }
            }
            sb.Append("</select>");
            return new HtmlString(sb.ToString());
        }

        public static HtmlString SelectOptionsData(this HtmlHelper helper, dynamic _dictionary = null) {
            return new HtmlString(RightControl.Common.SerializerHelper.JsonSerialized(_dictionary));
        }

        public static HtmlString TabaleHeadHtml(this HtmlHelper helper, dynamic _list = null, Boolean isEngish = false) {
            StringBuilder sb = new StringBuilder();
            var list = _list as List<ExportFields>;
            if (list != null && list.Count > 0) {
                string unitStr = "", fieldName = "";
                int index = 1;
                foreach (var item in list) {
                    if (item.FdType == 2 && !string.IsNullOrEmpty(item.Unit)) {
                        unitStr = string.Format("</br>({0})", item.Unit);
                    }
                    else {
                        unitStr = "";
                    }
                    fieldName = isEngish ? item.EngFieldNm : item.FieldName;

                    string template = "";

                    template = string.Format(", {{ field: '{0}', title: '<a class=\"hd\" isSize=\"{3}\" index=\"{4}\" href=\"#\">{1}</a>',width: {2}, templet: {{template}}, align: 'center'}}", fieldName, fieldName + unitStr, item.Width, item.Unit == "cm" ? "1" : "0", index++);

                    if (item.FdType == 2) {
                        sb.AppendLine(template.Replace("{template}", "'<div><input type=\"text\"" + (item.MustNotNull ? "lay-verify=\"required\" lay-vertype=\"tips\"" : "") + " id=\"prop_" + item.SortIndex + "\" class=\"layui-input\" autocomplete=\"off\" value=\"{{d." + fieldName + "}}\"/></div>'"));
                    }
                    else {
                        sb.AppendLine(template.Replace("{template}", "getOptions(\"" + fieldName + "\"" + "," + (item.MustNotNull ? 1 : 0) + ")"));
                    }
                }
            }
            return new HtmlString(sb.ToString().TrimEnd('\r').TrimEnd('\n'));
        }

        public static HtmlString CountryHtml(this HtmlHelper helper, string selectVal = "") {
            Dictionary<string, string> countryMap = new Dictionary<string, string>();
            countryMap.Add("US", "美国");
            countryMap.Add("JAP", "日本");
            countryMap.Add("CAN", "加拿大");
            string selectProp = "";
            StringBuilder sb = new StringBuilder(string.Format(@"<select id='selCategoryCode' name='CategoryCode'>
                                                                <option value = ''>请选择</option >"));
            foreach (var item in countryMap) {
                selectProp = selectVal == item.Key ? "selected" : "";
                sb.AppendFormat("<option value='{0}' {2}> {1} </option>", item.Key, item.Value, selectProp);
            }
            sb.Append("</select>");
            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// 运费模板
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="_list"></param>
        /// <param name="selectVal"></param>
        /// <returns></returns>
        public static HtmlString ShipHtml(this HtmlHelper helper, dynamic _list = null, string selectVal = "") {
            StringBuilder sb = new StringBuilder(string.Format(@"<select id='selShipCode' name='ShipCode'>
                                                                <option value = ''>请选择</option >"));
            var list = _list as List<ShipTemplate>;
            if (list != null && list.Count > 0) {
                string selectProp = "";
                foreach (var v in list) {
                    selectProp = selectVal == v.Id.ToString() ? "selected" : "";
                    sb.AppendFormat("<option value = '{0}' {2}> {1} </option>", v.Id, v.TemplateName, selectProp);
                }
            }
            sb.Append("</select>");
            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// 运费模板
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="_list"></param>
        /// <param name="selectVal"></param>
        /// <returns></returns>
        public static HtmlString AuthShopHtml(this HtmlHelper helper, dynamic _list = null, string selectVal = "") {
            StringBuilder sb = new StringBuilder(string.Format(@"<select id='selAuthShop' name='AuthId'  lay-filter='authShop'>"));
            var list = _list != null ? _list.data as List<PlatformAuth> : new List<PlatformAuth>();
            if (list != null && list.Count > 0) {
                string selectProp = "";
                for (int i = 0; i < list.Count; i++) {
                    selectProp = i == 0 ? "selected" : "";
                    sb.AppendFormat("<option value = '{0}' {2}> {1} </option>", list[i].Id, list[i].ShopName, selectProp);
                }
            }
            sb.Append("</select>");
            return new HtmlString(sb.ToString());
        }
    }
}